package legumes.client.util.datetime;

import java.util.Date;

public class DateTimeUtils {

	private static final int ISO_THURSDAY = 4; 
    private static final int MAX_DAY_OF_WEEK = 6; 
    private static final int DAYS_IN_WEEK = 7; 
    private static final long MILLIS_DAY = 86400000; 
    @SuppressWarnings("deprecation") 
    public static int getWeek(Date d_) { 
        // http://en.wikipedia.org/wiki/Talk:ISO_week_date#Algorithms 
        final Date date = d_; 
        // ISO week day (Mon=1 to Sun=7) 
        final int dayOfWeek = 1 + (date.getDay() + MAX_DAY_OF_WEEK) % DAYS_IN_WEEK; 
        final Date nearestThu = addDays(date, ISO_THURSDAY - dayOfWeek); 
        final int year = nearestThu.getYear(); 
        final Date jan1 = new Date(year, 0, 1); 
        return 1 + dayDiff(nearestThu, jan1) / DAYS_IN_WEEK; 
    } 
    public static Date addDays(final Date sourceDate, final long days) { 
        return new Date(sourceDate.getTime() + (days * MILLIS_DAY)); 
    } 
    public static int dayDiff(final Date firstDate, final Date secondDate) { 
        return (int) ((firstDate.getTime() - secondDate.getTime()) / MILLIS_DAY); 
    } 
}
